package com.baijiayun.liveuibase.devicetesting.fragment

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.view.*
import androidx.core.view.GravityCompat
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.baijiayun.liveuibase.R
import com.baijiayun.liveuibase.utils.DetachableClickListener
import com.baijiayun.liveuibase.utils.Precondition
import kotlinx.android.synthetic.main.bjy_base_fragment_device_testing_quit.*

class DeviceTestingDialogFragment : DialogFragment() {
    private var onButtonClickedListener: OnButtonClickedListener? = null
    private var mTitle: String? = null
    private var mContent: String? = null
    private var positiveText: String? = null
    private var negativeText: String? = null

    fun getLayoutId() = R.layout.bjy_base_fragment_device_testing_quit

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.bjy_base_fragment_device_testing_quit, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        bjy_base_device_testing_dialog_title_tv.text = mTitle
        bjy_base_device_testing_dialog_content_tv.text = mContent
        bjy_base_device_testing_dialog_positive_btn.text = positiveText
        bjy_base_device_testing_dialog_negative_btn.text = negativeText
        bjy_base_device_testing_dialog_positive_btn.setOnClickListener {
            onButtonClickedListener?.onPositive()
        }
        bjy_base_device_testing_dialog_negative_btn.setOnClickListener {
            onButtonClickedListener?.onNegative()
        }
        bjy_base_device_testing_dialog_background.setOnClickListener {
            dismissAllowingStateLoss()
        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = Dialog(activity!!, R.style.BJYBaseUIDialogFragmentStyle)
        Precondition.checkNotNull(dialog.window)
        dialog.setCanceledOnTouchOutside(true)
        dialog.setCancelable(true)
        dialog.window!!.requestFeature(Window.FEATURE_NO_TITLE)
        dialog.window!!.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
        dialog.window!!.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
        dialog.window!!.decorView.systemUiVisibility = activity!!.window.decorView.systemUiVisibility
        // 处理dialog沉浸式状态栏
        val detachableClickListener = DetachableClickListener.wrap { dialog1: DialogInterface? ->
            try {
                //Clear the not focusable flag from the window
                if (getDialog() == null || activity == null || activity!!.isDestroyed) return@wrap
                getDialog()!!.window!!.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
                //Update the WindowManager with the new attributes (no nicer way I know of to do this)..
                val wm = activity!!.getSystemService(Context.WINDOW_SERVICE) as WindowManager
                wm.updateViewLayout(getDialog()!!.window!!.decorView, getDialog()!!.window!!.attributes)
            } catch (ignore: Exception) {
                // do nothing
            }
        }
        dialog.setOnShowListener(detachableClickListener)
        detachableClickListener.clearOnDetach(dialog)
        return dialog
    }

    override fun show(manager: FragmentManager, tag: String?) {
        try {
            val mDismissed = DialogFragment::class.java.getDeclaredField("mDismissed")
            mDismissed.isAccessible = true
            mDismissed[this] = false
            val mShownByMe = DialogFragment::class.java.getDeclaredField("mShownByMe")
            mShownByMe.isAccessible = true
            mShownByMe[this] = true
            val ft = manager.beginTransaction()
            ft.add(this, tag)
            ft.commitAllowingStateLoss()
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    override fun onStart() {
        super.onStart()
        val window = dialog!!.window
        Precondition.checkNotNull(window)
        val windowParams = window?.attributes
        if (windowParams != null) {
            windowParams.dimAmount = 0.50f
            windowParams.flags = windowParams.flags or WindowManager.LayoutParams.FLAG_DIM_BEHIND
            setWindowParams(windowParams)
            window.attributes = windowParams
        }
    }

    private fun setWindowParams(windowParams: WindowManager.LayoutParams) {
        windowParams.width = WindowManager.LayoutParams.MATCH_PARENT
        windowParams.height = WindowManager.LayoutParams.MATCH_PARENT
        windowParams.gravity = Gravity.BOTTOM or GravityCompat.END
    }

    fun setTitle(title: String) {
        mTitle = title
    }

    fun setContent(content: String) {
        mContent = content
    }

    fun setPositiveButtonText(text: String) {
        positiveText = text
    }

    fun setNegativeButtonText(text: String) {
        negativeText = text
    }

    fun setButtonClickedListener(listener: OnButtonClickedListener) {
        this.onButtonClickedListener = listener
    }

    interface OnButtonClickedListener {
        fun onPositive()
        fun onNegative()
    }
}